package org.cs.mgr.before.dao.impl;

import java.util.List;
import java.util.Map;

import org.cs.core.dao.impl.BaseDao;
import org.cs.mgr.before.dao.IFindDao;
import org.cs.mgr.before.model.FindGoods;
import org.cs.mgr.before.model.Goods;
import org.cs.mgr.before.model.User;
import org.cs.mgr.before.service.IGoodsService;
import org.cs.mgr.before.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class FindDao extends BaseDao<FindGoods> implements IFindDao{
//注入service
	@Autowired
	IGoodsService goodService;
	@Autowired
	IUserService userService;
	
	@Override
	public List<Map<String, Object>> getFindGoods(String type) {
		String sql = null;		//根据条件分别查询发现列表
		if (type.equals("1")) {
         sql="select * from find_goods";
		}
		if (type.equals("2")) {
		 sql="select * from find_goods where FIND_IMAGE1!=''";	
		}
		if (type.equals("3")) {
		 sql="select * from find_goods where FIND_IMAGE1=null";	
		}
		if (type.equals("4")) {
		 sql="select * from find_goods order by ATTR1 desc";	
		}
		if (type.equals("5")) {
		 sql="select * from find_goods order by CREATE_TIME asc";	
		}
		if (type.equals("6")) {
		 sql="select * from find_goods order by CREATE_TIME desc";	
		}
		List<Map<String, Object>> findBySql = findBySql(sql);
	    for (Map<String, Object> map : findBySql) {
	    	String UID = (String)map.get("USER_ID");			//通过用户id得到用户的信息
			User user = userService.getUser(UID);
			map.put("uName", user.getUsername());
			map.put("uImg", user.getUserImg());
			String GOODID = (String)map.get("GOODS_ID");			//通过商品id得到商品信息
			Goods goodsOne = goodService.getGoodsOne(GOODID);
			map.put("gdetail", goodsOne.getGOOD_DETAILS());
			map.put("gPayNum", goodsOne.getPAY_NUM());
			map.put("gPic", goodsOne.getGOOD_PICTURE_URL());
			map.put("gImg", goodsOne.getGOOD_PICTURE_URL());
		}
		return findBySql;
	}

	@Override
	public List<Map<String, Object>> getFindGoods() {
		List<Map<String, Object>> findBySql = findBySql("select * from find_goods"); 
		for (Map<String, Object> map : findBySql) {
			String  uid = (String)map.get("USER_ID");
			User user = userService.getUser(uid);
			map.put("Id", user.getId());
		}
		return findBySql;
	}

}
